package io.renren.modules.sp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.SeasonFilter;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.constant.Constant;
import io.renren.modules.sp.dao.SpNoticeDao;
import io.renren.modules.sp.dto.SpMatchDTO;
import io.renren.modules.sp.dto.SpNoticeDTO;
import io.renren.modules.sp.entity.SpMatchEntity;
import io.renren.modules.sp.entity.SpNoticeEntity;
import io.renren.modules.sp.service.SpNoticeService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dao.SysCommDao;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 通知
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2023-07-26
 */
@Service
public class SpNoticeServiceImpl extends CrudServiceImpl<SpNoticeDao, SpNoticeEntity, SpNoticeDTO> implements SpNoticeService {

    @Autowired
    private SysCommDao sysCommDao;

    @Override
    public QueryWrapper<SpNoticeEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<SpNoticeEntity> wrapper = new QueryWrapper<>();

        String title = (String)params.get("title");
        wrapper.like(StringUtils.isNotBlank(title), "title", title);
        String type = (String)params.get("type");
        wrapper.eq(StringUtils.isNotBlank(type), "type", type);

        return wrapper;
    }


    @Override
    public PageData<Map> pageNotice(Map<String, Object> params) {
        Page page = createPage(params);
        //分页参数
        params.put(Constant.PAGE, page);
        IPage<Map<String,Object>> page2 = this.baseDao.pageNotice(page, params);
        PageData<Map> pageData = new PageData(page2.getRecords(),page2.getTotal());
        return pageData;
    }

    @Override
    @SeasonFilter(tableAlias = "mc")
    public PageData<Map> queryPage(Map<String, Object> params) {
        Page page = createPage(params);
        //分页参数
        params.put(Constant.PAGE, page);
        IPage<Map<String,Object>> page2 = this.baseDao.pageQuery(page, params);
        PageData<Map> pageData = new PageData(page2.getRecords(),page2.getTotal());
        return pageData;
    }

    @Override
    public Integer unReadCount(Map<String, Object> params){
        return this.baseDao.unReadCount(params);
    }

    /**
     * 赛项已读次数  >0 标识已读
     * @param itemId
     * @param userId
     */
    @Override
    public Integer readItemCount(Long itemId, Long userId){
        String sql = "SELECT count(1) FROM sp_notice_item_log t  WHERE t.item_id = #{p.itemId} AND t.user_id = #{p.userId} limit 0,1";
        Map<String,Object> param = new HashMap<>();
        param.put("itemId", itemId);
        param.put("userId", userId);
        return this.sysCommDao.count(sql, param);
    }


}